<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>青芽医疗管理平台</title>
    <meta name="description" content="数据采集分析系统">
    <meta name="keywords" content="数据采集分析系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
</head>
<body class="hold-transition">

<div id="app">
    <div class="filter-container">
        <el-input placeholder="患者名称" style="width: 200px;" class="filter-item" v-model="pagination.queryName"></el-input>
        <el-date-picker placeholder="开始时间" type="datetime" style="width: 200px;" v-model="pagination.beginTime"></el-date-picker>
        <el-date-picker placeholder="结束时间" type="datetime" style="width: 200px;" v-model="pagination.endTime"></el-date-picker>
        <el-button @click="_getQuestionOrderPage()" class="dalfBut">查询</el-button>
    </div>

    <el-table size="small" current-row-key="questionOrderId" :data="list" stripe highlight-current-row>
        <el-table-column type="index" align="center" label="序号"></el-table-column>
        <el-table-column prop="patientName" label="患者名称" align="center"></el-table-column>
        <el-table-column prop="patientDescription" label="患者描述" align="center"></el-table-column>
        <el-table-column prop="doctorAdvice" label="医嘱" align="center"></el-table-column>
        <el-table-column prop="medicalId" label="药品" align="center">
            <template slot-scope="scope">
                <el-button size="mini" type="primary" @click="showMedicalList(scope.row.medicalId)">查看</el-button>
            </template>
        </el-table-column>
        <el-table-column prop="formatQuestionTime" label="就诊时间" align="center"></el-table-column>
        <el-table-column prop="totalMoney" label="总金额" align="center"></el-table-column>
        <el-table-column prop="ifTakeMedicine" label="是否取药" align="center"></el-table-column>
        <el-table-column prop="formatPayTime" label="支付时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
            <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button size="mini" type="danger" @click="delQuestionOrder(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>

    <div class="add-form">
        <el-dialog title="查看药品" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
            <el-table size="small" current-row-key="medicalId" :data="medicalList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="medicalName" label="药品名称" align="center"></el-table-column>
                <el-table-column prop="medicalDescribe" label="药品描述" align="center"></el-table-column>
                <el-table-column prop="medicalPrice" label="药品价格" align="center"></el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible=false">关闭</el-button>
            </div>
        </el-dialog>
    </div>

    <div class="add-form">
        <el-dialog title="编辑问诊单" :visible.sync="dialogFormVisible4Edit"  width="1060px" :close-on-click-modal="false">
            <el-form ref="dataEditForm" :model="questionOrder"  label-position="right" label-width="100px">
                <el-row>
                    <el-form-item label="患者描述" prop="patientDescription">
                        <el-input v-model="questionOrder.patientDescription"/>
                    </el-form-item>
                    <el-form-item label="医嘱" prop="doctorAdvice">
                        <el-input v-model="questionOrder.doctorAdvice"/>
                    </el-form-item>
                    <el-form-item label="选择药品" prop="medicalId">
                        <el-button type="primary" @click="getMedicalPage">选择药品</el-button>
                        <el-table size="small" current-row-key="medicalId" :data="selectMedicalList" stripe highlight-current-row>
                            <el-table-column type="index" align="center" label="序号"></el-table-column>
                            <el-table-column prop="medicalName" label="药品名称" align="center"></el-table-column>
                            <el-table-column prop="medicalDescribe" label="药品描述" align="center"></el-table-column>
                            <el-table-column prop="medicalPrice" label="药品价格" align="center"></el-table-column>
                        </el-table>
                    </el-form-item>
                    <el-form-item label="总金额" prop="totalMoney">
                        <el-input v-model="questionOrder.totalMoney" disabled/>
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                <el-button type="primary" @click="updateQuestionOrder()">确定</el-button>
            </div>
        </el-dialog>
    </div>

    <div class="add-form">
        <el-dialog title="选择药品" :visible.sync="dialogFormVisibleMedical" :close-on-click-modal="false">
            <div class="filter-container">
                <el-input class="filter-item" placeholder="药品名称" style="width: 200px;" v-model="pagination.queryName"></el-input>
                <el-button @click="_getMedicalPage()" class="dalfBut">查询</el-button>
            </div>
            <el-table ref="multipleTable" size="small" current-row-key="medicalId" :data="medicalList" stripe highlight-current-row @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="medicalName" label="药品名称" align="center"></el-table-column>
                <el-table-column prop="medicalDescribe" label="药品描述" align="center"></el-table-column>
                <el-table-column prop="medicalPrice" label="药品价格" align="center"></el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        :total="pagination.total"
                        @current-change="handleCurrentChange"
                        class="pagiantion"
                        layout="total, prev, pager, next, jumper">
                </el-pagination>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleMedical=false">取消</el-button>
                <el-button type="primary" @click="addMedicalToTable()">提交</el-button>
            </div>
        </el-dialog>
    </div>
</div>

</body>
<!-- 引入组件库 -->
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script type="application/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            list:[],
            questionOrder:{},
            dialogFormVisible:false,
            dialogFormVisible4Edit:false,
            dialogFormVisibleMedical:false,
            medicalList:[],
            multipleSelection:[],
            selectMedicalList:[],
            handleSelectMedicalList:[],
        },
        mounted () {
            this.getQuestionOrderPage();
        },
        methods: {
            _getMedicalPage() {
                this.pagination.currentPage = 1;
                this.getMedicalPage();
            },
            getMedicalPage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                this.dialogFormVisibleMedical = true;
                var queryPageBean = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryTerm: {
                        medicalName: this.pagination.queryName
                    }
                };
                axios.post("/medical/getMedicalPage", "queryPageBean=" + JSON.stringify(queryPageBean)).then((res) => {
                    this.medicalList = res.data.list;
                    this.pagination.total = res.data.total;
                    this.medicalList.forEach(row => {
                        this.$nextTick(function () {
                            this.selectMedicalList.forEach(p=>{
                                if(p.medicalId==row.medicalId){
                                    this.$refs.multipleTable.toggleRowSelection(row,true);
                                }
                            });
                        });
                    });
                });
            },
            handleSelectionChange(val){
                this.handleSelectMedicalList=[];
                val.forEach(e => {
                    this.handleSelectMedicalList.push(e);
                })
            },
            addMedicalToTable(){
                this.dialogFormVisibleMedical = false;
                this.multipleSelection = [];
                this.selectMedicalList = this.handleSelectMedicalList;
                var num = 0;
                this.selectMedicalList.forEach(e =>{
                    this.multipleSelection.push(e.medicalId);
                    num += Number(e.medicalPrice);
                })
                this.questionOrder.medicalId = this.multipleSelection.toString().replace("\"","");
                this.questionOrder.totalMoney = num;
            },

            _getQuestionOrderPage() {
                this.pagination.currentPage = 1;
                this.getQuestionOrderPage();
            },
            getQuestionOrderPage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                var queryPageBean = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryTerm: {
                        patientName: this.pagination.queryName,
                        beginTime: formatDate(this.pagination.beginTime,"YYYY-MM-DD hh:mm:ss"),
                        endTime: formatDate(this.pagination.endTime,"YYYY-MM-DD hh:mm:ss")
                    }
                };
                axios.post("/questionOrder/getQuestionOrderPage", "queryPageBean=" + JSON.stringify(queryPageBean)).then((res) => {
                    this.list = res.data.list;
                    this.pagination.total = res.data.total;
                });
            },
            handleCurrentChange(currentPage) {
                //为模型数据赋值（当前页面）
                this.pagination.currentPage = currentPage;
                this.getQuestionOrderPage();
            },
            showMedicalList(row){
                this.dialogFormVisible = true;
                axios.post("/medical/getMedicalByIdList?ids="+row).then((res) => {
                    this.medicalList = res.data;
                });
            },
            handleUpdate(row){
                this.dialogFormVisible4Edit=true;
                axios.get("/questionOrder/getById?id="+row.questionOrderId).then((res) => {
                    this.questionOrder = res.data;
                    axios.post("/medical/getMedicalByIdList?ids="+this.questionOrder.medicalId).then((res) => {
                        this.selectMedicalList = res.data;
                    });
                });
            },
            updateQuestionOrder(){
                this.$confirm("你确定要修改当前数据吗？","提示",{//确认框
                    type:'warning'
                }).then(()=>{
                    axios.post("/questionOrder/updateQuestionOrder","questionOrder="+JSON.stringify(this.questionOrder)).then((res) => {
                        if(res){
                            this.$message({
                                type:'success',
                                message:"修改成功"
                            });
                            //重新进行分页查询
                            this.getQuestionOrderPage();
                        }else{
                            //执行失败
                            this.$message.error("修改失败");
                        }
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                }).catch(()=>{
                    this.$message({
                        type:'info',
                        message:'操作已取消'
                    });
                }).finally(() => {
                    //不管成功还是失败，都调用分页查询方法
                    this.getQuestionOrderPage();
                    //隐藏编辑窗口
                    this.dialogFormVisible4Edit = false;
                });
            },
            delQuestionOrder(row){
                this.$confirm("你确定要删除当前数据吗？","提示",{//确认框
                    type:'warning'
                }).then(()=>{
                    //用户点击确定按钮，发送ajax请求，将检查项ID提交到Controller进行处理
                    axios.post("/questionOrder/delQuestionOrder","questionOrder="+JSON.stringify(row)).then((res) => {
                        if(res.data){
                            //执行成功
                            //弹出成功提示信息
                            this.$message({
                                type:'success',
                                message:"删除成功"
                            });
                            //重新进行分页查询
                            this.getQuestionOrderPage();
                        }else{
                            //执行失败
                            this.$message.error("删除失败");
                        }
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                }).catch(()=>{
                    this.$message({
                        type:'info',
                        message:'操作已取消'
                    });
                });
            }
        }
    })
    function formatDate(date, format) {
        if(!(date instanceof Date)) {
            console.error("请传入正确的日期对象")
            return ""
        }
        const map = {
            "Y+": date.getFullYear(), // 年
            "M+": date.getMonth() + 1, // 月份
            "D+": date.getDate(), // 日
            "h+": date.getHours(), // 小时
            "m+": date.getMinutes(), // 分
            "s+": date.getSeconds() // 秒
        }

        for(let key in map) {
            let reg = new RegExp(`(${key})`)
            if(reg.test(format)) {
                let str = map[key].toString()
                format = format.replace(RegExp.$1, str.padStart(RegExp.$1.length, "0"))
            }
        }

        return format
    }
</script>
</html>